package com.yanqu.road.server.cmd.banquet.cross;

import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.banquet.UserCrossBanquetData;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_BANQUET_CROSS_JOIN_RESP, desc = "加入宴会返回")
public class CrossBanquetJoinRespCmd extends AbstractCommand {

	private int res;//返回的结果
	private String banquetId;
	private int giftId;
	private int pos;
	private String reward = "";
	private String giftTypeAccept = "";
	private String autoJoinInfo = "";

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossBanquetProto.JoinCrossBanquetResp reqMsg = CrossBanquetProto.JoinCrossBanquetResp.parseFrom(packet.getBytes());
		res = reqMsg.getRet();
		banquetId = reqMsg.getBanquetId();
		giftId = reqMsg.getGiftId();
		pos = reqMsg.getPos();
		giftTypeAccept = reqMsg.getGiftTypeAccept();
		autoJoinInfo =  reqMsg.getAutoJoinInfo();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		UserCrossBanquetData banquet = CrossBanquetMgr.getBanquet(banquetId);
		if(banquet == null){
			ret = GameErrorCode.E_BANQUET_IS_OVER;
			return;
		}
		BanquetGiftInfo banquetGiftInfo = BanquetConfigMgr.getBanquetGiftInfo(banquet.getType(),giftId);
		if(banquetGiftInfo == null){
			ret = GameErrorCode.E_BANQUET_GIFT_ERROR;
			return;
		}
		if(!"0".equals(banquetGiftInfo.getJoinConsume())){
			player.getModule(PreReduceModule.class).restorePreReduce(banquetGiftInfo.getJoinConsume());
		}
		if(res != 0){
			ret = res;
		}else{
			reward = CrossBanquetMgr.joinCrossBanquet(player,banquetId,giftId,pos);
			BanquetMgr.addBanquetJoinTimes(player);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		BanquetProto.JoinBanquetResp.Builder respMsg = BanquetProto.JoinBanquetResp.newBuilder();
		respMsg.setRet(ret);
		respMsg.setReward(reward);
		respMsg.setPos(pos);
		respMsg.setGiftTypeAccept(giftTypeAccept);
		respMsg.setAutoJoinInfo(autoJoinInfo);
		player.sendPacket(Protocol.U_BANQUET_CROSS_JOIN, respMsg);
	}

}
